<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    .box1{width: 100px;height: 100px;background: red;position: absolute;left:0px;top:0;}
    .box2{width: 100px;height: 100px;background: green;position: absolute;left:0px;top:110px;}
    .line{width: 1px;background: black;position: absolute;left:400px;top:0;height: 400px;}
  </style>
</head>
<body>
  <div class="box1"></div>
  <div class="box2"></div>
  <div class="line"></div>
</body>
<script>

  

  const box1 = document.querySelector(".box1");
  const box2 = document.querySelector(".box2");
  
  box1.onmouseover = function(){
    move(box1, "left", 300);
  }
  
  box2.onmouseover = function(){
    move(box2, "width", 400);
  }

  // 封装缓冲运动：任意元素任意属性运动到任意目标
  
  // 功能：多元素单个不同属性任意目标缓冲运动
  function move(ele, attr, target){
    if(ele.t) return;
    // 将每个计时器的返回值都保存到当前元素身上，每个元素都需要有一个独立的计时器控制
    ele.t = setInterval(()=>{
      // 获取要操作的属性的当前值
      let now = parseInt(getStyle(ele, attr));
      let speed = (target - now) / 10;
      speed = speed < 0 ? Math.floor(speed) : Math.ceil(speed);
      if(target === now){
        clearInterval(ele.t);
        ele.t = undefined;
      }else{
        ele.style[attr] = now + speed + "px";
      }
    }, 30);
  }



  function getStyle(ele, attr){
    if(ele.currentStyle){
      return ele.currentStyle[attr];
    }else{
      return getComputedStyle(ele)[attr];
    }
  }




  
</script>
</html>